home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / ChiSquare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-01  |  1.3 KB  |  59 lines  |  [TEXT/KAHL]

  1. /*
  2. ChiSquare.c
  3. Copyright 1990-1992 © Denis G. Pelli
  4. PChiSquare() calculates the significance level at which a fit may be rejected.
  5.  
  6. Also see: Binomial.c, Exponential.c, Normal.c, Uniform.c
  7.  
  8. HISTORY:
  9. 1/7/84    dgp    wrote it in FORTRAN
  10. 8/9/89    dgp    translated it to C
  11. 4/5/90    dgp    tested it against tables in Abramowitz in Stegun. Works fine.
  12. 4/8/90    dgp    renamed file ChiSquare.c from PChiSquare.c
  13. 7/30/91    dgp    now use NAN defined in VideoToolbox.h
  14. 1/12/92    dgp    Renamed NormalPDF() to NormalPdf().
  15. */
  16. #include "VideoToolbox.h"
  17. #include <math.h>
  18.  
  19. double PChiSquare (double chiSquare,int n)
  20. /*
  21. Returns one minus the cumulative probability of the
  22. Chi-Square distribution for value chisq with n degrees of freedom.
  23. The formula is from Abramowitz and Stegun, Eqs. 26.4.4 and 26.4.5, pg 941.
  24. The formula is exact.
  25. Range: chisq>=0.0, n>=0,
  26. Returns zero if n is zero.
  27.  
  28. M. Abramowitz and I.A. Stegun (1964) Handbook of mathematical functions. Dover.
  29. */
  30. {
  31.     double x,a,P;
  32.     int i,ii;
  33.     
  34.     if(chiSquare<0.0 || n<0) return NAN;    /* return NAN to signal error */
  35.     x = sqrt(chiSquare);
  36.     if(n%2 != 0) {
  37.         /* n is odd */
  38.         P = 2.0*Normal(-x);
  39.         a = 2.0*NormalPdf(x)*x;
  40.         ii = (n-1)/2;
  41.         for(i=1;i<=ii;i++){
  42.             P += a;
  43.             a *= chiSquare/(2*i+1);
  44.         }
  45.     }
  46.     else {
  47.         /* n is even */
  48.         P = 0.0;
  49.         a = sqrt(2.0*PI)*NormalPdf(x);
  50.         ii = n/2;
  51.         for( i=1;i<=ii;i++){
  52.             P += a;
  53.             a *= chiSquare/(2*i);
  54.         }
  55.     }
  56.     return P;
  57. }
  58.  
  59.